/**
 * 
 */
package com.piz.web.order;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import com.piz.model.Ship_Addr;
import com.piz.service.model.ConfirmOrderBean;

/**
 * @author prameetk.sharma
 *
 */
@Component("confirmOrderValidation")
public class ConfirmOrderValidation  implements org.springframework.validation.Validator{
	
	
	private static final String EMAIL_PATTERN = 
         "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private static final String CHAR_ONLY = "^[a-zA-Z]+$";
	private static final String NUMBER_ONLY = "^[0-9]+$";
	private Pattern pattern;
	  private Matcher matcher;
	  
	@Override
	public boolean supports(Class<?> klass) {
		return ConfirmOrderBean.class.isAssignableFrom(klass);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ConfirmOrderBean bean = (ConfirmOrderBean) target;
		Ship_Addr shipAddr = bean.getShipAddr();
		
		if(bean.getPayment_type_id() == null)
		{
			errors.rejectValue("payment_type_id","","Payment Type is required");
		}
		if(bean.getBank_id() == null)
		{
			errors.rejectValue("bank_id","","Bank is required");
		}
		if(shipAddr != null){
		if(!StringUtils.hasText(shipAddr.getAddr1()) ||  shipAddr.getAddr1().equals("Address 1"))
		{
			errors.rejectValue("shipAddr.addr1","","Addr1 is required.");
		}
		if(!StringUtils.hasText(shipAddr.getCity()) ||  shipAddr.getCity().equals("City"))
		{
			errors.rejectValue("shipAddr.city","","city is required");
		}
		else if (!shipAddr.getCity().matches(CHAR_ONLY)) {
			errors.rejectValue("shipAddr.city","","city is not valid. Enter only characters.");
		}
		if(!StringUtils.hasText(shipAddr.getState()) ||  shipAddr.getState().equals("State"))
		{
			errors.rejectValue("shipAddr.state","","State is required");
		}
		else if (!shipAddr.getState().matches(CHAR_ONLY)) {
			errors.rejectValue("shipAddr.state","","State is not valid. Enter only characters.");
		}
		if(!StringUtils.hasText(shipAddr.getFn()) ||  shipAddr.getFn().equals("First Name"))
		{
			errors.rejectValue("shipAddr.fn","","First Name is required.");
		}
		else if(!shipAddr.getFn().matches(CHAR_ONLY))
		{
			errors.rejectValue("shipAddr.fn","","First Name is not valid. Enter Characters only.");
		}
		if(!StringUtils.hasText(shipAddr.getLn()) ||  shipAddr.getLn().equals("Last Name"))
		{
			errors.rejectValue("shipAddr.ln","","Last Name is required");
		}
		else if(!shipAddr.getLn().matches(CHAR_ONLY))
		{
			errors.rejectValue("shipAddr.ln","","Last Name is not valid. Enter Characters only.");
		}
		if(!StringUtils.hasText(shipAddr.getMobile()) ||  shipAddr.getMobile().equals("Mobile Number"))
		{
			errors.rejectValue("shipAddr.mobile","","Mobile is required");
		}
		else if (!shipAddr.getMobile().matches(NUMBER_ONLY)) {
			errors.rejectValue("shipAddr.mobile","","Mobile is not valid. Enter only numeric characters.");
		}
		 /**
		   * Validate hex with regular expression
		   * @param hex hex for validation
		   * @return true valid hex, false invalid hex
		   */
		if(StringUtils.hasText(shipAddr.getEmail()) ||  !shipAddr.getEmail().equals("Email"))
		{
			pattern = Pattern.compile(EMAIL_PATTERN);
			matcher = pattern.matcher(shipAddr.getEmail());
			if(!matcher.matches()) 
			{
				errors.rejectValue("shipAddr.email","","EmailId is not valid.");
			}
		}
		}
		try {
	          errors.pushNestedPath("shipAddr");
	          
	      } finally {
	          errors.popNestedPath();
	      }
	}

}
